<?php

/**
 * login actions.
 *
 * @package    mgm_system
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
 		$this->error = "";
		$user = $this->getRequestParameter("user");
		$password = $this->getRequestParameter("password");
		
		$directory_id = $request->getParameter("sl_master_directory");

		$user = Doctrine_Core::getTable('User')->getUser($user, md5($password));
		
		$this->directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();

		if ($user != null){
			
			$this->getUser()->setAttribute("user_id", $user->getId());
			
			$this->getUser()->setAttribute("rol_id", $user->getUserRolId());
			
			$this->getUser()->setAttribute("user_name", $user->getName());
			
			$this->getUser()->addCredential($user->getUserRol()->getName());

			$this->getUser()->setAuthenticated(true);
			return $this->redirect("select_directory/index");
		}
		else if ($password != "") {
			$this->error = "Invalid credentials";
		}
  }
  
  
	public function executeLogout(sfWebRequest $request){
		
		$this->getUser()->setAttribute("user", "");

		$this->getUser()->setAuthenticated(false);
		
		$this->getUser()->clearCredentials();
		
		$this->getUser()->getAttributeHolder()->clear();

		return $this->redirect("login/index");
	}
  
}
